package cc.orange.mainView;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import androidx.viewpager2.widget.ViewPager2;
import cc.orange.TestPagerAdapter;
import cc.orange.adapter.PersonImgAdapter;
import cc.orange.base.BaseActivity;
import cc.orange.entity.BaseEntity;
import cc.orange.entity.MsgaIntroduceEntity;
import cc.orange.entity.PersonMsgEntity;
import cc.orange.entity.ReturnImageEntity;
import cc.orange.entity.ReturnWmEntity;
import cc.orange.http.ApiService;
import cc.orange.utils.StatusBarUtilView;

import cc.orange.databinding.ActivityMsgBinding;

import cc.orange.R;

import cc.orange.utils.ZXToastUtil;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.tendcloud.tenddata.TalkingDataSDK;
import com.xcheng.retrofit.Call;
import com.xcheng.retrofit.DefaultCallback;
import com.xcheng.retrofit.HttpError;
import com.xcheng.retrofit.RetrofitFactory;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

public class MsgActivity extends BaseActivity implements View.OnClickListener{

    private ActivityMsgBinding binding;
    private List<Fragment> fragments=new ArrayList<>();
    private PersonImgAdapter imgAdapter;//顶部图片
    private ArrayList<PersonMsgEntity.Data.Image>list_img = new ArrayList<>();
    private MyFragmentAdapet myFragmentAdapet;
    private TestPagerAdapter adapterPages;
    private PersonMsgEntity personMsgEntity;
    private ImageView [] img_count ;
    private String uid;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_msg);
        uid = getIntent().getStringExtra("uid");
        initViews();
        returnOne();

        EventBus.getDefault().register(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getIntroduce(MsgaIntroduceEntity entity){
        if(!TextUtils.isEmpty(entity.getIntroduce())){
            binding.msgaText3.setText(entity.getIntroduce());
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }

    private void initViews() {
        binding.msgaRel1.setOnClickListener(this);
        binding.msgaRel2.setOnClickListener(this);
        binding.mineText11.setOnClickListener(this);
        binding.msgaRel5.setOnClickListener(this);
        binding.msgaRel41.setOnClickListener(this);

        img_count = new ImageView[]{binding.msgaImg1,binding.msgaImg2,binding.msgaImg3,
                binding.msgaImg4,binding.msgaImg5};
        fragments.add(new MsgInfoFragment(uid));
//        fragments.add(new Discover1Fragment(2,uid));
//        fragments.add(new AppraiseFragment());

        adapterPages = new TestPagerAdapter(getSupportFragmentManager(),getLifecycle(), fragments);

//        myFragmentAdapet=new MyFragmentAdapet(MsgActivity.this,fragments);
        binding.vpVpAs.setAdapter(adapterPages);
        binding.vpVpAs.setOffscreenPageLimit(1);
        binding.vpVpAs.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels);
            }

            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                initTab(position);
            }
        });

        imgAdapter = new PersonImgAdapter(list_img,this);
        binding.vpMsga1.setAdapter(imgAdapter);

       binding.vpMsga1.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
           @Override
           public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

           }

           @Override
           public void onPageSelected(int position) {
//               binding.msgaText12.setText((position + 1)+"/"+list_img.size());
           }

           @Override
           public void onPageScrollStateChanged(int state) {

           }
       });
    }

    @Override
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        StatusBarUtilView.transparencyBar(MsgActivity.this);
        StatusBarUtilView.setLightStatusBar(MsgActivity.this, true, true);

        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) binding.msgaRel5.getLayoutParams();
        layoutParams.setMargins(30,getStatusBarHeight()+50,0,0);
        binding.msgaRel5.setLayoutParams(layoutParams);
    }

    private void initTab(int type){
        binding.msgaText6.setVisibility(View.GONE);
        binding.msgaText8.setVisibility(View.GONE);

        binding.msgaText5.setTextColor(getResources().getColor(R.color.color_666666));
        binding.msgaText7.setTextColor(getResources().getColor(R.color.color_666666));

        if(type == 0){
            binding.msgaText6.setVisibility(View.VISIBLE);
            binding.msgaText5.setTextColor(getResources().getColor(R.color.color_333));
        }

        if(type == 1){
            binding.msgaText8.setVisibility(View.VISIBLE);
            binding.msgaText7.setTextColor(getResources().getColor(R.color.color_333));
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.msga_rel4_1:
                if(null == personMsgEntity || null == personMsgEntity.getData().getNumber())return;
                Intent intent2 = new Intent(MsgActivity.this, ConversationListActivity.class);
                intent2.putExtra("uid",personMsgEntity.getData().getNumber());
                intent2.putExtra("name",personMsgEntity.getData().getName());
                startActivity(intent2);
                break;
            case R.id.msga_rel1:
                initTab(0);
                binding.vpVpAs.setCurrentItem(0);
//                binding.msgaRel4.setVisibility(View.GONE);
                break;
            case R.id.msga_rel2:
                initTab(1);
                binding.vpVpAs.setCurrentItem(1);
//                binding.msgaRel4.setVisibility(View.VISIBLE);
                break;
            case R.id.mine_text11:
                if(null != personMsgEntity){
                    attention(""+personMsgEntity.getData().getNumber(),
                            personMsgEntity.getData().getGuanZhu() == 0 ? 1 : 0);//0 已关注
                }
                break;
            case R.id.msga_rel5:
                finish();
                break;
        }
    }


    private void returnOne(){
        RetrofitFactory.create(ApiService.class)
                .returnOne(getTokens(),uid)
                .enqueue(new DefaultCallback<PersonMsgEntity>() {
                    @Override
                    public void onStart(Call<PersonMsgEntity> call) {
                        showLoading_base(MsgActivity.this);
                    }

                    @Override
                    public void onError(Call<PersonMsgEntity> call, HttpError error) {
                        cancelLoading();
                    }

                    @Override
                    public void onSuccess(Call<PersonMsgEntity> call, PersonMsgEntity baseEntity) {
                        cancelLoading();
                        if(baseEntity.getCode() == 0){
                            personMsgEntity = baseEntity;
                            setData4View();
                        }else{
                            ZXToastUtil.showToast(baseEntity.getMsg());
                        }
                    }
                });
    }

    private void setData4View() {
        binding.msgaText1.setText(personMsgEntity.getData().getName());
        binding.msgaText2.setText(personMsgEntity.getData().getName());
//        binding.msgaText3.setText("ID："+personMsgEntity.getData().getId());
            binding.discItemRel1.setBackgroundResource(personMsgEntity.getData().getSex() == 0 ?
                    R.drawable.icon_disc_sex0 : R.drawable.icon_disc_sex1);
        binding.discItemRel1.setText(""+personMsgEntity.getData().getAge());
            binding.discItemRel2.setVisibility(personMsgEntity.getData().getIsPerson() == 0 ?
                    View.VISIBLE : View.GONE);

            binding.discItemRel3.setVisibility(personMsgEntity.getData().getIsVip() == 0 ?
                    View.VISIBLE : View.GONE);

//        binding.mineText3.setText("LV."+personMsgEntity.getData().getLevel());
        getImg(""+personMsgEntity.getData().getLevel());

        binding.mineText11.setImageResource(personMsgEntity.getData().getGuanZhu() == 0 ?
                R.drawable.icon_msga_attion_img2:R.drawable.icon_msga_attion_img1);
//        binding.msgaText11.setText(personMsgEntity.getData().getUserNum()+"分");
//        for (int i = 1; i < 6; i++) {
//            if(personMsgEntity.getData().getUserNum() >= i){
//                img_count[i-1].setImageResource(R.drawable.icon_msga_img1);
//            }else if(personMsgEntity.getData().getUserNum() < i && personMsgEntity.getData().getUserNum() > i-1){
//                img_count[i-1].setImageResource(R.drawable.icon_msga_img2_1);
//            } else{
//                img_count[i-1].setImageResource(R.drawable.icon_msga_img2);
//            }
//        }
        if(null != personMsgEntity.getData().getImage()) {
            list_img.addAll(personMsgEntity.getData().getImage());
        }

//        binding.msgaText12.setText("1/"+list_img.size());
        imgAdapter.notifyDataSetChanged();

        EventBus.getDefault().postSticky(personMsgEntity);
    }

    private void attention(String uid,int flag){
        RetrofitFactory.create(ApiService.class)
                .attention(getTokens(),uid,""+flag)
                .enqueue(new DefaultCallback<BaseEntity>() {
                    @Override
                    public void onStart(Call<BaseEntity> call) {
                        showLoading_base(MsgActivity.this);
                    }

                    @Override
                    public void onError(Call<BaseEntity> call, HttpError error) {
                        cancelLoading();
                    }

                    @Override
                    public void onSuccess(Call<BaseEntity> call, BaseEntity baseEntity) {
                        cancelLoading();
                        if(baseEntity.getCode() == 0){
                            personMsgEntity.getData().setGuanZhu(flag);
                            binding.mineText11.setImageResource(flag == 0 ?
                                    R.drawable.icon_msga_attion_img2:R.drawable.icon_msga_attion_img1);
                        }else{
                            ZXToastUtil.showToast(baseEntity.getMsg());
                        }
                    }
                });
    }

    @Override
    protected void onResume() {
        super.onResume();
        TalkingDataSDK.onPageBegin(this, getLocalClassName());
    }

    @Override
    protected void onPause() {
        super.onPause();
        TalkingDataSDK.onPageEnd(this, getLocalClassName());
    }

    private void getImg(String se){
        switch (se) {
            case "1" :
                Glide.with(MsgActivity.this).load(R.drawable.icon_lv1).into(binding.msgaImg6);
                break;
            case "2" :
                Glide.with(MsgActivity.this).load(R.drawable.icon_lv2).into(binding.msgaImg6);
                break;
            case "3" :
                Glide.with(MsgActivity.this).load(R.drawable.icon_lv3).into(binding.msgaImg6);
                break;
            case "4" :
                Glide.with(MsgActivity.this).load(R.drawable.icon_lv4).into(binding.msgaImg6);
                break;
            case "5" :
                Glide.with(MsgActivity.this).load(R.drawable.icon_lv5).into(binding.msgaImg6);
                break;
            case "6" :
                Glide.with(MsgActivity.this).load(R.drawable.icon_lv6).into(binding.msgaImg6);
                break;
            case "7" :
                Glide.with(MsgActivity.this).load(R.drawable.icon_lv7).into(binding.msgaImg6);
                break;
            case "8" :
                Glide.with(MsgActivity.this).load(R.drawable.icon_lv8).into(binding.msgaImg6);
                break;
            case "LV.9" :
                Glide.with(MsgActivity.this).load(R.drawable.icon_lv9).into(binding.msgaImg6);
                break;
            case "LV.10" :
                Glide.with(MsgActivity.this).load(R.drawable.icon_lv10).into(binding.msgaImg6);
                break;
        }
    }
}